package com.live.dengdengtv;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.live.dengdengtv.bean.SimpleInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.SimpleInfoMap;
import com.live.dengdengtv.net.callback.ProgressDialogCallBack;
import com.live.dengdengtv.net.callback.RequestNetCallback;
import com.live.dengdengtv.utils.Lo;
import com.live.dengdengtv.utils.Notice;
import com.live.dengdengtv.view.DialogWaiting;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class BaseActivity extends AppCompatActivity implements RequestNetCallback {
    protected Toolbar mToolbar;
    protected Activity context = this;
    ;

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_base);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);

//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });
//    }

    protected void setToolbar() {
        setToolbar(true);
    }

    protected void setToolbar(boolean backIcon) {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);

//        mToolbar.setSubtitle(R.string.skip);
//        mToolbar.setTitle(R.string.skip);
        mToolbar.setTitle("跳过");

        setSupportActionBar(mToolbar);

        if (backIcon) {

            mToolbar.setNavigationIcon(R.mipmap.back);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

        }
    }


    protected void setToolbar(String title) {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
//        TextView cusTitle_tv = (TextView) findViewById(R.id.center_tv);

//        mToolbar.setSubtitle(R.string.skip);
//        mToolbar.setTitle(R.string.skip);
        mToolbar.setTitle("");
//        cusTitle_tv.setText(custitle);

        setSupportActionBar(mToolbar);



    }
    protected void setToolbar(boolean backIcon, int leftStringResid, String custitle) {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView cusTitle_tv = (TextView) findViewById(R.id.center_tv);

//        mToolbar.setTitle(R.string.skip);
        mToolbar.setTitle(leftStringResid);
        cusTitle_tv.setText(custitle);

        setSupportActionBar(mToolbar);

        if (backIcon) {

            mToolbar.setNavigationIcon(R.mipmap.back);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

        }

    }

    private int callId = -1;
    protected int generCallId(){
        return ++callId;
    }
    protected void requestData(String url, Map<String, String> params){
        requestNetData(url, params, ++callId,true);
    }
    protected void requestData(String url, Map<String, String> params,int id){
        requestNetData(url, params, id, true);
    }
    protected void requestNetData(String url, Map<String, String> params,int id, boolean isShowProgress){

        Callback callback;
        if(isShowProgress){
            callback = new ProgressDialogCallBack(this,this);
        }else {
            callback = new ProgressDialogCallBack(null,this);
        }
        OkHttpUtils.post().id(id).tag(this.getClass().getName()).url(url).params(params).build().execute(callback);
    }

    @Override
    public void onBefore(Request request, int id) {

    }

    @Override
    public void onAfter(int id) {

    }

    @Override
    public void inProgress(float progress, long total, int id) {

    }

    @Override
    public void onError(Call call, Exception e, int id) {
        Notice.alert(context, e.getMessage());
    }

    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this.getClass().getName());
    }

    public abstract class DialogCallBack<T> extends Callback<T> {
        protected DialogWaiting progressDialog;

        public void onBefore(Request request, int id) {
            progressDialog = DialogWaiting.show((Context) context);
        }

        public void onAfter(int id) {
            progressDialog.dismiss();
        }
    }

    public abstract class BaseInfoCallback extends BaseActivity.DialogCallBack<SimpleInfo> {
        @Override
        public SimpleInfo parseNetworkResponse(Response response, int id) throws Exception {

            String string = response.body().string();

            Lo.e(getTag(), string);

            SimpleInfo baseInfoBean = new Gson().fromJson(string, SimpleInfo.class);
            return baseInfoBean;
        }
//        public abstract void onResponse(SimpleInfo response, int id);
        public String getTag() {
            return "BaseInfoCallback";
        }
    }

    public abstract class BaseInfoMapCallback extends BaseActivity.DialogCallBack<SimpleInfoMap> {
        @Override
        public SimpleInfoMap parseNetworkResponse(Response response, int id) throws Exception {

            String string = response.body().string();

            Lo.e(getTag(), string);

            SimpleInfoMap baseInfoBean = new Gson().fromJson(string, SimpleInfoMap.class);
            return baseInfoBean;
        }
        public String getTag(){
            return "BaseInfoMapCallback";
        }
    }
}